package com.google.code.irouter.portal.client.dashboard.main.center.managers.relation.browser.editor.detail.consumer;

import java.util.List;

import com.google.code.irouter.portal.client.AppConstants;
import com.google.code.irouter.portal.client.dashboard.main.center.managers.relation.browser.editor.detail.consumer.source.Command;
import com.google.code.irouter.portal.client.dashboard.main.center.managers.relation.browser.editor.detail.consumer.source.command.Tag;
import com.google.code.irouter.portal.client.rpc.consumer.ConsumerResult;
import com.google.code.irouter.portal.client.rpc.consumer.ConsumerService;
import com.google.code.irouter.portal.client.rpc.consumer.ConsumerServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.Ext;
import com.gwtext.client.data.SimpleStore;
import com.gwtext.client.data.Store;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.GridPanel;

public final class Source extends GridPanel {
	
	private static final AppConstants CONSTANTS = GWT.create(AppConstants.class);
	
	private final ConsumerServiceAsync consumerService = GWT.create(ConsumerService.class);
	
	public static final String GROUP = "Consumer";

	public Source() {
		super();
		this.setId(Source.class.getName());
		
		this.setWidth(300);
		this.setEnableDragDrop(true);
		this.setDdGroup(GROUP);
		this.setBottomToolbar(new Command());
		ColumnConfig[] columns = {
				new ColumnConfig(CONSTANTS.com_google_code_irouter_portal_client_dashboard_main_center_managers_relation_browser_editor_detail_consumer_source_column_name(),"name", 100, true),
				new ColumnConfig(CONSTANTS.com_google_code_irouter_portal_client_dashboard_main_center_managers_relation_browser_editor_detail_consumer_source_column_sender(),"sender", 200, true)
		};
		ColumnModel columnModel = new ColumnModel(columns);
		this.setColumnModel(columnModel);
		Object[][] consumers = new Object[0][0];
		Store store = new SimpleStore(new String[] { "name", "sender", "consumerKey", "user", "maxLength", "key" }, consumers);
		store.load();
		this.setStore(store);
	}
	
	public void refresh() {
		getEl().mask(CONSTANTS.com_google_code_irouter_portal_client_common_processing(), true);
		Tag tag = (Tag) Ext.getCmp(Tag.class.getName());
		consumerService.load(
				tag.getText(),
				new AsyncCallback<List<ConsumerResult>>() {
					public void onFailure(Throwable caught) {
					// TODO
						getEl().unmask();
					}

					public void onSuccess(List<ConsumerResult> consumerResultList) {
						Object[][] consumerArray = new Object[consumerResultList.size()][6];
						int index = 0;
						for (ConsumerResult consumerResult : consumerResultList) {
							Object[] consumerItem = new Object[6];
							consumerItem[0] = consumerResult.getName();
							consumerItem[1] = consumerResult.getSender();
							consumerItem[2] = consumerResult.getConsumerKey();
							consumerItem[3] = consumerResult.getUser();
							consumerItem[4] = consumerResult.getMaxLength();
							consumerItem[5] = String.valueOf(consumerResult.getId());
							consumerArray[index] = consumerItem;
							index++;
						}
						Store store = new SimpleStore(
								new String[] { 
										"name",
										"sender",
										"consumerKey", 
										"user", 
										"maxLength", 
										"key"
								}, 
								consumerArray
						);
						store.load();
						reconfigure(store, getColumnModel());
						getEl().unmask();
					}
				}
		);
	}
	
	public void reset() {
		Object[][] consumers = new Object[0][0];
		Store store = new SimpleStore(new String[] { "name", "sender", "consumerKey", "user", "maxLength", "key" }, consumers);
		store.load();
		if(this.isRendered()) {
			this.reconfigure(store, this.getColumnModel());
			((Tag) Ext.getCmp(Tag.class.getName())).setValue("");
		} else {
			this.setStore(store);
			this.setColumnModel(this.getColumnModel());
		}
	}

}
